
# 此脚本只是在机器上安装完成kubeadm/kubectl/kubelet
# 后续配置需要再配. 
# 比如加入节点的  网桥转发,需要手工配置.

echo "insetall kubernets"

check(){
	# 检查,确保可以正常安装. 
	counter=`yum list installed | grep wget | wc -l`
	if [ $counter -lt 1 ] ;
	then 	
		echo "wget 未安装"
		yum install -y wget
	fi
	
	# docker 判断是否启动
    # TODO

	# swap 关闭
	counter2=`free -m | grep Swap | awk '{print $2}'`
	if [ $counter2 -gt 0 ] ;
        then
                echo "swap 未关闭,请先关闭"
                exit 1 
        fi
	# selinux关闭检查
	selinuxstatus=`getenforce`
	if [ $selinuxstatus -eq 'Disabled' ] ;
        then
                echo "selinux 未关闭,请先关闭"
                exit 1 
        fi
}

# 配置源
config_k8ssource(){
	cd /etc/yum.repos.d
	touch /etc/yum.repos.d/kubernets.repo

	echo "[kubernetes]" > /etc/yum.repos.d/kubernets.repo
	echo "name=kubernetes Repo " >> /etc/yum.repos.d/kubernets.repo
	echo "baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ " >> /etc/yum.repos.d/kubernets.repo
	echo "gpgcheck=1 " >> /etc/yum.repos.d/kubernets.repo
	echo "gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg " >> /etc/yum.repos.d/kubernets.repo
	echo "enabled=1 " >> /etc/yum.repos.d/kubernets.repo

	wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
	rpm --import rpm-package-key.gpg
}

# 安装k8s
install_k8s(){
	cd ~
	yum -y install kubeadm-$1 kubectl-$1 kubelet-$1 kubernetes-cni-$2
}

# 准备镜像
down_images(){
	url=registry.cn-hangzhou.aliyuncs.com/google_containers
	version=$1
	images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
	for imagename in ${images[@]} ; do
  		docker pull $url/$imagename
  		docker tag $url/$imagename k8s.gcr.io/$imagename
  		docker rmi -f $url/$imagename
	done
}

down_images_node(){
	docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:$1
	docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:$1 k8s.gcr.io/kube-proxy:$1
	docker rmi -f registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:$1

	docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$2
	docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$2 k8s.gcr.io/pause:$2
	docker rmi -f registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$2
}



# 安装k8s
check
config_k8ssource
install_k8s 1.13.1 0.6.0
kubeadm version
